package com.ruoyi.system.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.system.domain.Cutarea;
import com.ruoyi.system.mapper.CutareaMapper;
import org.apache.commons.collections.map.HashedMap;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.ruoyi.system.constant.Constants.BASE_DIR;
import static com.ruoyi.system.constant.Constants.PixelCount;
import static com.ruoyi.system.utils.cutPicture.cut;

public class resultmap {
    //CDR判分
    static Map<Double,Integer>mp=new HashMap<>();
    static double[] num = { 0, 0.5, 1, 2, 3 };
    static double[] res = new double[6];
    static Double score=null;
    static Cutarea cutarea;
    static String cutareajson;
    Map<String,Double> map = new HashMap<>();
    static JSONArray jsonArray;

    public static void solve() {
        double m = res[0];
        int cnt1 = 0; // 等于
        int cnt2 = 0; // 大于
        int cnt3 = 0; // 小于
        int cnt4 = 0; // 大于等于1
        int cnt5 = 0; // 大于等于0.5
        boolean flag = true;
        int[] cnt = new int[5];
        for (int i = 0; i < 5; i++) {
            cnt[i] = 0;
        }

        for (int i = 1; i <= 5; i++) {
            if (res[i] == m) {
                cnt1++;
            } else if (res[i] > m) {
                cnt2++;
            } else {
                cnt3++;
            }
            int count = mp.get(res[i]);
            cnt[count]++;
            if (res[i] >= 1) {
                cnt4++;
            }
            if (res[i] >= 0.5) {
                cnt5++;
            }
        }

        // Scoring logic
        if (cnt1 >= 3) {
            score = m;
            flag = false;
        } else if (cnt2 >= 3 || cnt3 >= 3) {
            int tmpcnt = 0;
            for (int i = 0; i < 5; i++) {
                if (cnt[i] > tmpcnt) {
                    tmpcnt = cnt[i];
                    score = num[i];
                    flag = false;
                } else if (cnt[i] == tmpcnt) {
                    flag = true; // not satisfied
                }
            }
        }

        if (flag) {
            if ((cnt2 == 3 && cnt3 == 2) || (cnt2 == 2 && cnt3 == 3)) {
                score = m;
            } else if (m == 0.5) {
                if (cnt4 >= 3) {
                    score = 1.0;
                } else {
                    score = 0.5;
                }
            } else if (m == 0) {
                if (cnt5 >= 2) {
                    score = 0.5;
                } else {
                    score = 0.0;
                }
            } else if ((cnt1 == 1 || cnt1 == 2) && cnt2 <= 2 && cnt3 <= 2) {
                score = m;
            } else if (m == 1 && cnt[0] >= 3) {
                score = 0.5;
            } else {
                double minDiff = 4;
                for (int i = 1; i <= 5; i++) {
                    if (Math.abs(res[i] - m) != 0 && Math.abs(res[i] - m) < minDiff) {
                        minDiff = Math.abs(res[i] - m);
                        score = res[i];
                    }
                }
            }
        }
    }
    public static Map<String,Double> retrunresmap(Map<String,Double> map,Long scrid) {
        Map<String,Double> newmap = new HashMap<>();
        //初始化mp
        mp.put(0.0, 0);
        mp.put(0.5, 1);
        mp.put(1.0, 1);
        mp.put(2.0, 2);
        mp.put(3.0, 4);
        //CDR
        boolean tmpflag=false;
        for(int i=1;i<=6;i++){
            if(map.containsKey("CDR"+i)){
                res[i-1]=map.get("CDR"+i).doubleValue();
                tmpflag=true;
            }
        }
        if(tmpflag){
            solve();
            newmap.put(("CDR"+" total"),score);
        }
        else{
            newmap.put(("CDR"+" total"),null);
        }
        //DST
        Double dst1 = null;
        Double dst2 = null;
        for(Map.Entry<String,Double> entry : map.entrySet()) {
            if (entry.getKey().contains("DST1")) {
                if (dst1 == null) {
                    dst1 = 0.0;
                }
                dst1 = Double.max(dst1, entry.getValue());
            }
            if (entry.getKey().contains("DST2")) {
                if (dst2 == null) {
                    dst2 = 0.0;
                }
                dst2 = Double.max(dst2, entry.getValue());
            }
        }
        newmap.put(("DST1 total"),dst1);
        newmap.put(("DST2 total"),dst2);


        //NPI
        Double total1 = null;
        Double total2 = null;
        for(int i=1;i<=12;i++) {
            if (map.containsKey("NPI" + i + "-2") && map.containsKey("NPI" + i + "-3")) {
                if (total1 == null) {
                    total1 = 0.0;
                }
                total1 = total1 + map.get("NPI" + i + "-2").doubleValue() * map.get("NPI" + i + "-3").doubleValue();
            }
            if (map.containsKey("NPI" + i + "-4")) {
                if (total2 == null) {
                    total2 = 0.0;
                }
                total2 = total2 + map.get("NPI" + i + "-4").doubleValue();
            }
        }
        newmap.put(("NPI1 total"),total1);
        newmap.put(("NPI2 total"),total2);



        Double mmse = null;
        Double gds = null;
        Double his = null;
        Double psqi = null;
        Double scd = null;
        Double moca =null;
        Double adl = null;
        Double aqd1 = null;
        Double aqd2 = null;
        Double CFTA = null;
        Double CFTB = null;
        Double CFTC = null;
        Double CFT = null;
        Double CDT = null;
        Double SCDS = null;
        Double HAMD=null;
        Double MMSE_directive=null;
        Double MMSE_memory=null;
        Double MMSE_attention=null;
        Double MMSE_recall=null;
        Double MMSE_language=null;
        Double HAMA=null;
        Double moca1=null;
        Double moca2=null;
        Double moca3=null;
        Double moca4=null;
        Double moca5=null;
        Double moca6=null;
        Double moca7=null;
        Double moca8=null;
        Double avlt1=null;
        Double avlt2=null;
        Double avlt3=null;
        Double avlt4=null;
        Double avlt5=null;
        Double avlt6=null;
        Double avlt7=null;




        for(Map.Entry<String,Double> entry : map.entrySet()){
            if(entry.getKey().contains("MMSE")){
                if(mmse==null){
                    mmse=0.0;
                }
                mmse = mmse + entry.getValue();
            }
            if(entry.getKey().contains("MMSE1-")){
                if(MMSE_directive==null){
                    MMSE_directive=0.0;
                }
                MMSE_directive = MMSE_directive + entry.getValue();
            }
            if(entry.getKey().contains("MMSE2-")){
                if(MMSE_memory==null){
                    MMSE_memory=0.0;
                }
                MMSE_memory = MMSE_memory + entry.getValue();
            }
            if(entry.getKey().contains("MMSE3-")){
                if(MMSE_attention==null){
                    MMSE_attention=0.0;
                }
                MMSE_attention = MMSE_attention + entry.getValue();
            }
            if(entry.getKey().contains("MMSE4-")){
                if(MMSE_recall==null){
                    MMSE_recall=0.0;
                }
                MMSE_recall = MMSE_recall + entry.getValue();
            }
            if(entry.getKey().contains("MMSE5-")){
                if(MMSE_language==null){
                    MMSE_language=0.0;
                }
                MMSE_language = MMSE_language + entry.getValue();
            }
            if(entry.getKey().contains("GDS")){
                if(gds==null){
                    gds=0.0;
                }
                gds = gds + entry.getValue();
            }
            if (entry.getKey().contains("HIS")){
                if(his==null){
                    his=0.0;
                }
                his = his + entry.getValue();
            }
            if (entry.getKey().contains("PSQI")){
                if(psqi==null){
                    psqi=0.0;
                }
                psqi = psqi + entry.getValue();
            }
            if (entry.getKey().contains("SCD")){
                if(scd==null){
                    scd=0.0;
                }
                scd = scd + entry.getValue();
            }
            if (entry.getKey().contains("MOCA")){
                if(moca==null){
                    moca=0.0;
                }
                moca = moca + entry.getValue();
            }
            if (entry.getKey().contains("MOCA1-")){
                if(moca1==null){
                    moca1=0.0;
                }
                moca1 = moca1 + entry.getValue();
            }
            if (entry.getKey().contains("MOCA2-")){
                if(moca2==null){
                    moca2=0.0;
                }
                moca2 = moca2 + entry.getValue();
            }
            if (entry.getKey().contains("MOCA3-")){
                if(moca3==null){
                    moca3=0.0;
                }
                moca3 = moca3 + entry.getValue();
            }
            if (entry.getKey().contains("MOCA4-")){
                if(moca4==null){
                    moca4=0.0;
                }
                moca4 = moca4 + entry.getValue();
            }
            if (entry.getKey().contains("MOCA5-")){
                if(moca5==null){
                    moca5=0.0;
                }
                moca5 = moca5 + entry.getValue();
            }
            if (entry.getKey().contains("MOCA6-")){
                if(moca6==null){
                    moca6=0.0;
                }
                moca6 = moca6 + entry.getValue();
            }
            if (entry.getKey().contains("MOCA7-")){
                if(moca7==null){
                    moca7=0.0;
                }
                moca7 = moca7 + entry.getValue();
            }
            if (entry.getKey().contains("MOCA8-")){
                if(moca8==null){
                    moca8=0.0;
                }
                moca8 = moca8 + entry.getValue();
            }
            if (entry.getKey().contains("ADL")){
                if(adl==null){
                    adl=0.0;
                }
                adl = adl + entry.getValue();
            }
            if (entry.getKey().contains("AQD1")){
                if(aqd1==null){
                    aqd1=0.0;
                }
                aqd1 = aqd1 + entry.getValue();
            }
            if (entry.getKey().contains("AQD2")){
                if(aqd2==null){
                    aqd2=0.0;
                }
                aqd2 = aqd2 + entry.getValue();
            }

            if (entry.getKey().contains("CFTA")){
                if(CFTA==null){
                    CFTA=0.0;
                }
                CFTA = CFTA + entry.getValue();
            }
            if (entry.getKey().contains("CFTB")){
                if(CFTB==null){
                    CFTB=0.0;
                }
                CFTB = CFTB + entry.getValue();
            }
            if (entry.getKey().contains("CFTC")){
                if(CFTC==null){
                    CFTC=0.0;
                }
                CFTC = CFTC + entry.getValue();
            }
            if(entry.getKey().contains("CDT")){
                if(CDT==null){
                    CDT=0.0;
                }
                CDT = CDT + entry.getValue();
            }
            if(entry.getKey().contains("SCDS")){
                if(SCDS==null){
                    SCDS=0.0;
                }
                SCDS = SCDS + entry.getValue();
            }
            if(entry.getKey().contains("HAMD")){
                if(HAMD==null){
                    HAMD=0.0;
                }
                HAMD = HAMD + entry.getValue();
            }
            if(entry.getKey().contains("HAMA")){
                if(HAMA==null){
                    HAMA=0.0;
                }
                HAMA = HAMA + entry.getValue();
            }
            if(entry.getKey().contains("AVLT1-")){
                if(avlt1==null){
                    avlt1=0.0;
                }
                avlt1 = avlt1 + entry.getValue();
            }
            if(entry.getKey().contains("AVLT2-")){
                if(avlt2==null){
                    avlt2=0.0;
                }
                avlt2 = avlt2 + entry.getValue();
            }
            if(entry.getKey().contains("AVLT3-")){
                if(avlt3==null){
                    avlt3=0.0;
                }
                avlt3 = avlt3 + entry.getValue();
            }
            if(entry.getKey().contains("AVLT4-")){
                if(avlt4==null){
                    avlt4=0.0;
                }
                avlt4 = avlt4 + entry.getValue();
            }
            if(entry.getKey().contains("AVLT5-")){
                if(avlt5==null){
                    avlt5=0.0;
                }
                avlt5 = avlt5 + entry.getValue();
            }
            if(entry.getKey().contains("AVLT6-")){
                if(avlt6==null){
                    avlt6=0.0;
                }
                avlt6 = avlt6 + entry.getValue();
            }
            if(entry.getKey().contains("AVLT7-")){
                if(avlt7==null){
                    avlt7=0.0;
                }
                avlt7 = avlt7 + entry.getValue();
            }
        }
        if(CFTA!=null){
            if(CFT==null){
                CFT=0.0;
            }
            CFT=CFT+CFTA;
        }
        if(CFTB!=null){
            if(CFT==null){
                CFT=0.0;
            }
            CFT=CFT+CFTB;
        }
        if(CFTC!=null){
            if(CFT==null){
                CFT=0.0;
            }
            CFT=CFT+CFTC;
        }

        newmap.put("MMSE total",mmse);
        newmap.put("MMSE-directive",MMSE_directive);
        newmap.put("MMSE-memory",MMSE_memory);
        newmap.put("MMSE-attention",MMSE_attention);
        newmap.put("MMSE-recall",MMSE_recall);
        newmap.put("MMSE-language",MMSE_language);
        newmap.put("MOCA1 total",moca1);
        newmap.put("MOCA2 total",moca2);
        newmap.put("MOCA3 total",moca3);
        newmap.put("MOCA4 total",moca4);
        newmap.put("MOCA5 total",moca5);
        newmap.put("MOCA6 total",moca6);
        newmap.put("MOCA7 total",moca7);
        newmap.put("MOCA8 total",moca8);
        newmap.put("AVLT-N1 total",avlt1);
        newmap.put("AVLT-N2 total",avlt2);
        newmap.put("AVLT-N3 total",avlt3);
        newmap.put("AVLT-N4 total",avlt4);
        newmap.put("AVLT-N5 total",avlt5);
        newmap.put("AVLT-N6 total",avlt6);
        newmap.put("AVLT-N7 total",avlt7);
        newmap.put("GDS total",gds);
        newmap.put("HIS total", his);
        newmap.put("PSQI total", psqi);
        newmap.put("SCD total", scd);
        newmap.put("MOCA total", moca);
        newmap.put("ADL total", adl);
        newmap.put("AQD1 total", aqd1);
        newmap.put("AQD2 total", aqd2);
        newmap.put("CFTA total", CFTA);
        newmap.put("CFTB total", CFTB);
        newmap.put("CFTC total", CFTC);
        newmap.put("CFT total", CFT);
        newmap.put("CDT total", CDT);
        newmap.put("SCDS total", SCDS);
        newmap.put("HAMD total",HAMD);
        newmap.put("HAMA total",HAMA);
        //切图片，OCR得到所有切割区域分数
//        cutarea=CutareaMapper.selectCutareaById(1L);
//        System.out.println(cutareajson);
//        cutareajson=cutarea.getCutarea();
        cutareajson="[{\"id\":55249,\"name\":\"SCDS total\",\"x1\":670,\"y1\":1650,\"x2\":1455,\"y2\":1815},{\"id\":55232,\"name\":\"id\",\"x1\":1060,\"y1\":30,\"x2\":2100,\"y2\":100},{\"id\":55234,\"name\":\"id\",\"x1\":1060,\"y1\":30,\"x2\":2100,\"y2\":100},{\"id\":55236,\"name\":\"id\",\"x1\":1060,\"y1\":30,\"x2\":2100,\"y2\":100},{\"id\":55238,\"name\":\"id\",\"x1\":1060,\"y1\":30,\"x2\":2100,\"y2\":100},{\"id\":55240,\"name\":\"id\",\"x1\":1060,\"y1\":30,\"x2\":2100,\"y2\":100},{\"id\":55259,\"name\":\"TMT1A time\",\"x1\":580,\"y1\":2430,\"x2\":1060,\"y2\":2570},{\"id\":55259,\"name\":\"TMT1B time\",\"x1\":580,\"y1\":2570,\"x2\":1060,\"y2\":2700},{\"id\":55261,\"name\":\"TMT2A time\",\"x1\":575,\"y1\":2420,\"x2\":1060,\"y2\":2570},{\"id\":55261,\"name\":\"TMT2B time\",\"x1\":575,\"y1\":2570,\"x2\":1060,\"y2\":2700},{\"id\":55264,\"name\":\"VFTA1 total\",\"x1\":510,\"y1\":2160,\"x2\":1520,\"y2\":2300},{\"id\":55264,\"name\":\"VFTA2 total\",\"x1\":540,\"y1\":2305,\"x2\":1520,\"y2\":2400},{\"id\":55265,\"name\":\"VFTB1 total\",\"x1\":510,\"y1\":2160,\"x2\":1520,\"y2\":2300},{\"id\":55265,\"name\":\"VFTB2 total\",\"x1\":540,\"y1\":2305,\"x2\":1520,\"y2\":2400},{\"id\":55266,\"name\":\"VFTC1 total\",\"x1\":510,\"y1\":2160,\"x2\":1520,\"y2\":2300},{\"id\":55266,\"name\":\"VFTC2 total\",\"x1\":540,\"y1\":2305,\"x2\":1520,\"y2\":2400}]";
        jsonArray = new JSONArray(cutareajson);//所有切割区域信息json

        for(int i = 0; i < jsonArray.size(); i++){
            JSONObject point = jsonArray.getJSONObject(i);

            // 获取点的信息
            long id = point.getLong("id");
            String name= point.getStr("name");
            int x1 = point.getInt("x1");
            int y1 = point.getInt("y1");
            int x2 = point.getInt("x2");
            int y2 = point.getInt("y2");
            System.out.println(BASE_DIR+scrid+File.separator+"base_"+scrid+"_"+id+".png");
            System.out.println(BASE_DIR+scrid+File.separator+name+".png");
            try{
                cut(x1,y1,x2,y2,BASE_DIR+scrid+File.separator+"base_"+scrid+"_"+id+".png",BASE_DIR+scrid+File.separator+name+".png");
                cut(x1,y1,x2,y2,BASE_DIR+scrid+File.separator+scrid+"_"+id+".png",BASE_DIR+scrid+File.separator+name+"_score"+".png");
                String DetecteResult=OCR.GeneralHandwritingOCR(BASE_DIR+scrid+File.separator+name+"_score"+".png");
                Map<String,String>mp= JSON.parseObject(DetecteResult, new TypeReference<Map<String, String>>() {
                });
                Map<String,String>mpp=JSON.parseObject(mp.get("Response"), new TypeReference<Map<String, String>>() {
                });
                JSONArray textDetections = new JSONArray(mpp.get("TextDetections"));
                JSONObject res = textDetections.getJSONObject(0);
                System.out.println(res);
                Double score = null;
                try {
                    score = res.getDouble("DetectedText");
                } catch (NullPointerException e) {

                }
                if(score!=null)newmap.put(name, score);
            } catch (IOException | NoSuchAlgorithmException | InvalidKeyException e) {
                e.printStackTrace();
            }
        }

        return newmap;



    }
}
